package com.hqyj.cloudTest.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.cloudTest.dao.CityDao;
import com.hqyj.cloudTest.service.CityService;
import com.hqyj.entity.Test.City;
import com.hqyj.entity.common.vo.Result;
import com.hqyj.entity.common.vo.Search;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * personal demo
 **/

@Service
public class CityServiceImpl implements CityService {

    @Autowired
    private CityDao cityDao;


    @Value("${server.port}")
    private int serverPort;



    @Override
    @Transactional
    public Result<City> insertCity(City city) {
        City temp = cityDao.getCityByCityName(city.getCityName());
        if(temp!=null){
            return new Result<>(Result.ResultStatus.FAILED.code,"city name is repeated");
        }

        city.setDateCreated(new Date());
        city.setDateModified(new Date());

        cityDao.insertCity(city);

        return new Result<>(Result.ResultStatus.SUCCESS.code,"insert successfully",city);
    }

    @Override
    @Transactional
    public Result<City> updateCity(City city) {
        City temp = cityDao.getCityByCityId(city.getCityId());
        if(temp==null){
            return new Result<>(Result.ResultStatus.FAILED.code,"city does not exist!");
        }
        city.setDateModified(city.getDateModified());
        cityDao.updateCity(city);
        return new Result<>(Result.ResultStatus.SUCCESS.code,"update successfully",city);
    }

    @Override
    @Transactional
    public Result<Object> deleteCity(int Id) {
        City temp = cityDao.getCityByCityId(Id);
        if(temp==null){
            return new Result<>(Result.ResultStatus.FAILED.code,"city does not exist!");
        }
        cityDao.deleteCity(Id);
        return new Result<>(Result.ResultStatus.SUCCESS.code,"delete successfully");
    }

    @Override
    @Transactional
    public City selectCityById(int id) {
        System.out.println("===================== "+serverPort+" =====================");
//        int i=1/0;

        return cityDao.getCityByCityId(id);
    }

    @Override
    @Transactional
    public PageInfo<City> getCitiesBySearch(Search search) {
        search.init();
        PageHelper.startPage(search.getCurrentPage(),search.getPageSize());
        return PageInfo.of(Optional.ofNullable
                (cityDao.getCitiesBySearch(search)).orElse(Collections.emptyList()));
    }




    @Override
    public List<City> getCitiesByCountryId(Integer id) {
        return Optional.ofNullable(cityDao.getCitiesByCountryId(id))
                .orElse(Collections.emptyList());
    }
}
